package com.zhentao.controller;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;

@Slf4j
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    QwenChatModel chatModel;

    @Autowired
    QwenStreamingChatModel streamingChatModel;
    @Autowired
    private View error;


    @RequestMapping("/chat")
    public String test(@RequestParam(defaultValue = "你们有谁呀") String message){
        String chat = chatModel.chat(message);
        log.info("chat:{}",chat);
        return chat;
    }

    @RequestMapping("/steam")
    public String stream(@RequestParam(defaultValue = "给我说一个方言") String message){
        streamingChatModel.chat(message, new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String partialResponse) {
                System.out.println(partialResponse);
            }

            @Override
            public void onCompleteResponse(ChatResponse chatResponse) {
                System.err.println("[结束]");
            }

            @Override
            public void onError(Throwable throwable) {
                log.error(String.valueOf(throwable));
            }
        });
        return "chat";
    }
}
